﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ExcelDataImporter
{
    public class SheetTabPage : TabPage
    {
        private readonly string sheetName;
        private DataGridView dataGridView;
        private bool isDataInit;
        private DataTable sheetData;

        public SheetTabPage(string text) : base(text)
        {
            sheetName = text;
            InitializeComponent();
        }

        public string SheetName
        {
            get { return sheetName; }
        }

        public bool IsDataInit
        {
            get { return isDataInit; }
        }

        private void InitializeComponent()
        {
            dataGridView = new DataGridView();
            ((ISupportInitialize) (dataGridView)).BeginInit();
            SuspendLayout();

            dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            dataGridView.Location = new Point(0, 0);
            dataGridView.Name = "dataGridView";
            dataGridView.Size = new Size(240, 150);
            dataGridView.TabIndex = 0;
            dataGridView.Dock = DockStyle.Fill;
            ((ISupportInitialize) (dataGridView)).EndInit();

            Controls.Add(dataGridView);

            ResumeLayout(false);
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            if (Visible && !isDataInit)
            {
                isDataInit = true;
                sheetData = OleDbHelper.ReadSheetContent(Session.Current.ExcelFile, sheetName, 50,null);
                dataGridView.DataSource = sheetData;
            }
            base.OnVisibleChanged(e);
        }
    }
}